﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Extensions;
using FwProject.Controllers;
using FwProject.PrePaymentInfo;
using FwProject.PrePaymentInfo.Dto;
using FwProject.Settlement;
using FwProject.Settlement.Dto;
using FwProject.Users;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace FwProject.Web.Mvc.Controllers
{
    public class PaymentController : FwProjectControllerBase
    {
        private readonly IPaymentInfoAppService _paymentInfoAppService;
        private readonly IReceiptInfoAppService _receiptInfoAppService;
        private readonly ISettlementInfoAppService _settlementInfoAppService;
        private readonly IUserAppService _userAppService;
        private readonly IHostingEnvironment _env;

        public PaymentController(IPaymentInfoAppService paymentInfoAppService, IUserAppService userAppService, IReceiptInfoAppService receiptInfoAppService, ISettlementInfoAppService settlementInfoAppService, IHostingEnvironment env)
        {
            _paymentInfoAppService = paymentInfoAppService;
            _userAppService = userAppService;
            _receiptInfoAppService = receiptInfoAppService;
            _settlementInfoAppService = settlementInfoAppService;
            _env = env;
        }

        /// <summary>
        /// 获取所有经办人
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetAgentPeoperData()
        {
            var model = await _userAppService.GetAllAgentPeoperAsync();
            return Json(model);
        }
        /// <summary>
        /// 缴款凭证列表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index(int pageIdx, int size, DateTime? beginTime, DateTime? endTime, long? AgentPeoperUserId, long? PaymentPerson, string RestaurantName = "", string PaymentNumber = "")
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 10;

            if (Request.IsAjaxRequest())
            {
                var model = await _paymentInfoAppService.GetPagedListAsync(pageIdx, size, beginTime, endTime, AgentPeoperUserId, PaymentPerson, PaymentNumber);
                return PartialView("IndexPartial", model);
            }

            return View();
        }
        public async Task<IActionResult> ExportExcel(DateTime? beginTime, DateTime? endTime, long? AgentPeoperUserId, long? PaymentPerson, string RestaurantName = "", string PaymentNumber = "")
        {
            var list = await _paymentInfoAppService.GetAllListAsync(beginTime, endTime, AgentPeoperUserId, PaymentPerson, PaymentNumber);
            return DoExcel<PaymentInfoDto>(list, _env);
        }



        /// <summary>
        /// 缴款列表
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <returns></returns>
        public async Task<IActionResult> PaymentIndex(int pageIdx, int size, long? AgentPeoperUserId)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;
            var model = await _settlementInfoAppService.GetAllPaymentInfo(pageIdx, size, AgentPeoperUserId);
            if (Request.IsAjaxRequest())
            {
                return PartialView("PaymentIndexPartial", model);
            }
            return View(model);
        }

        /// <summary>
        /// 导出缴款列表
        /// </summary>
        /// <param name="AgentPeoperUserId"></param>
        /// <returns></returns>
        public async Task<IActionResult> ExportPaymentIndex(long? AgentPeoperUserId)
        {
            var list = await _settlementInfoAppService.GetAllPaymentInfoListAsync(AgentPeoperUserId);
            return DoExcel<SettlementInfoPrePaymentInfo>(list, _env);
        }


        /// <summary>
        /// 收款详情
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ReceiptDetail(long id, DateTime? beginTime, DateTime? endTime, ReceiptInfoStatus? ReceiptInfoStatus)
        {
            ViewBag.Id = id;
            var model = await _receiptInfoAppService.GetReceiptDetailsAsync(id, beginTime, endTime, ReceiptInfoStatus);
            if (Request.IsAjaxRequest())
            {
                return PartialView("ReceiptDetailPartial", model);
            }
            return View(model);
        }
        /// <summary>
        /// 交账
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IActionResult> Paymenting(Guid[] che)
        {
            var Id = await _paymentInfoAppService.Create(che);//缴款Id
            return Json(Id);
            //return Content($"<script>window.open('/Payment/PaymentPrint/{Id}');</script>");

        }
        /// <summary>
        /// 打印单据
        /// </summary>
        /// <param name="che"></param>
        /// <returns></returns>
        public async Task<IActionResult> PaymentPrint(Guid Id)
        {
            var model = await _paymentInfoAppService.GetByReceiptInfoIdAsync(Id);
            return View(model);
        }
    }
}